﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = Username.Text;
            string pass = Passsword.Text;
            string gender = Gender_female.Checked ? "1" : "0";
            string email = Email.Text;

            string constr = "sever =.;uid =sa;database = Student_db";
            SqlConnection con = new SqlConnection(constr);

            string sql = "insert into StudentInfo(stu_name,gender,email,[password])values (@name,@gender,@email,@pass)";
            SqlParameter[] pars =
                {
                new SqlParameter("@name",name),
                new SqlParameter("@pass",pass),
                new SqlParameter("@gender",gender),
                new SqlParameter("@email",email),
            };

            try
            {
                con.Open();
                SqlCommand com = new SqlCommand(sql, con);
                com.Parameters.AddRange(pars);

                int result = com.ExecuteNonQuery();

                if (result != -1)
                {
                    Literal1.Text = "注册成功，<a href ='Login.aspx'>登录</a>";
                }
                else
                {

                    Literal1.Text = "注册失败!";
                }

            }
            catch (Exception x)
            {

                throw new Exception(x.Message);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }
    }
}